package com.gmrz.uaf.servlet.v1.handler;

import com.gmrz.uaf.common.Constants;
import com.gmrz.uaf.common.GuiceUtil;
import com.gmrz.uaf.protocol.v1.json.UAFSchemaBuilder;
import com.gmrz.uaf.protocol.v1.processor.UAFDeviceCostedProcessor;
import com.gmrz.uaf.server.ServerContext;
import com.gmrz.uaf.servlet.v1.request.DeviceCostedContext;
import com.gmrz.uaf.servlet.v1.request.ReceiveReqPayload;
import com.gmrz.uaf.servlet.v1.response.FidoRespPayload;
import com.gmrz.uaf.servlet.v1.response.FidoRespPayloadDeviceCosted;
import com.gmrz.uaf.servlet.v1.validation.PayloadValidationException;
import com.gmrz.uaf.servlet.v1.validation.RestValidator;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Type;

/**
 * @PACKAGE_NAME: com.gmrz.uaf.servlet.v1.handler
 * @ClassName: UAFDeviceCostedHandler
 * @Description: 计费设备查询 handler
 * @Date: 2020-04-28 16:03
 * @Author: liyl@gmrz-bj.com
 **/
public class UAFDeviceCostedHandler extends UAFBaseHandler {

    private static final Logger LOG = LogManager.getLogger(UAFDeviceCostedHandler.class);


    /**
     * @param request
     * {
     *"  context":{
           "securityLevel":"1",
           "deviceID": "HWxkVQWERTYUIOPASDFGHJKLZXCVBNMQWERTYUIOPASDFGHJKL10"
        }
     }
     *
     * @return
     *{
        "Iscosted": 1,
        "description": "OK",
        "statusCode": 1200
     }
     *
     */
    public String deviceCosted(String request){
        LOG.info("[/device/costed][begin]");
        ServerContext.getServerContext().getFidoLog().setRequest(request);
        // 响应类
        FidoRespPayloadDeviceCosted respPayload = new FidoRespPayloadDeviceCosted();
        String response = null;

        try {

            Gson gson = UAFSchemaBuilder.getGson();
            Type reqType = new TypeToken<ReceiveReqPayload<DeviceCostedContext>>() {  }.getType();

            // 将请求串转为对象
            ReceiveReqPayload<DeviceCostedContext> reqPayload =gson.fromJson(request, reqType);

            // 记录日志
            logRequest(request, reqPayload.getContext().getTransNo(),null,
                    null, null,null, Constants.Method.DEVICE_COSTED);

            // 校验参数
            validate(reqPayload);

            UAFDeviceCostedProcessor processor = GuiceUtil.getProcessorInjector().
                    getInstance(UAFDeviceCostedProcessor.class);

            // 业务逻辑
            int isCosted = processor.deviceCostedSearch(reqPayload.getContext().getDeviceID(),
                    reqPayload.getContext().getSecurityLevel());

            // 构造响应
            respPayload.setStatusCode(1200);
            respPayload.setDescription("OK");
            respPayload.setIscosted(isCosted);
            return gson.toJson(respPayload);

        }catch (Exception ex) {
            LOG.error(ex);
            FidoRespPayload respPayloadEX = new FidoRespPayload();
            response = buildFidoRespFromException(respPayloadEX, ex);
        }
        LOG.info("[/device/costed][end]");
        return response;
    }


    void validate(ReceiveReqPayload<DeviceCostedContext> reqPayload) throws PayloadValidationException {
        if ((null == reqPayload) || (null == reqPayload.getContext())) {
            throw new PayloadValidationException("context", RestValidator.VALIDATION_ERROR.EMPTY);
        }
        RestValidator.validate("deviceID", reqPayload.getContext().getDeviceID());
        RestValidator.validate("securityLevel", reqPayload.getContext().getSecurityLevel());
    }
}
